package com.znsb.msfq.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.znsb.msfq.BaseFragment;
import com.znsb.msfq.R;
import com.znsb.msfq.activity.CommodityDetailsActivity;
import com.znsb.msfq.activity.ForgetPwdActivity;
import com.znsb.msfq.activity.WebViewActivity;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.bean.UserInfoBean;
import com.znsb.msfq.push.JPushUtils;
import com.znsb.msfq.utils.ActivityUtil;
import com.znsb.msfq.utils.ConfigUtils;
import com.znsb.msfq.utils.GsonUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.IntentUtils;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.SPUtils;
import com.znsb.msfq.utils.UrlUtils;
import com.znsb.msfq.utils.ValidateUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/9
 * @modifyTime： 2017/2/9
 * @explain：
 */

public class CommonLoginFragment extends BaseFragment {
    @Bind(R.id.common_tv_phone)
    TextView commonTvPhone;
    @Bind(R.id.common_edit_phone)
    EditText commonEditPhone;
    @Bind(R.id.common_tv_code)
    TextView commonTvCode;
    @Bind(R.id.common_edit_code)
    EditText commonEditCode;
    @Bind(R.id.common_tv_forgetpwd)
    TextView commonTvForgetpwd;
    @Bind(R.id.common_tv_login)
    TextView commonTvLogin;
    @Bind(R.id.common_checkBox)
    CheckBox commonCheckBox;
    @Bind(R.id.common_tv_pro)
    TextView commonTvPro;
    UserInfoBean data;

    @Override
    protected int getlayoutView() {
        return R.layout.frag_login_common;
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        String tempStr = "《马上分期用户协议》";
        SpannableStringBuilder style = new SpannableStringBuilder(tempStr);
        style.setSpan(new ForegroundColorSpan(Color.rgb(153, 153, 153)), 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.rgb(255, 64, 80)), 1, tempStr.length() - 1,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);// 设置指定位置文字的颜色5
        commonTvPro.setText(style);
        commonCheckBox.setChecked(true);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickListener(View view) {

    }


    @OnClick({R.id.common_tv_forgetpwd, R.id.common_tv_login, R.id.common_tv_pro})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_tv_forgetpwd:
                IntentUtils.startActivityAnimGeneral(getActivity(), ForgetPwdActivity.class, null);
                break;
            case R.id.common_tv_login:
                String phone = commonEditPhone.getText().toString().trim();
                String pwd = commonEditCode.getText().toString().trim();
                boolean isChecked = commonCheckBox.isChecked();
                if (ValidateUtils.isLogin(phone, null, pwd, isChecked))
                    setLogin(phone, pwd);
                break;
            case R.id.common_tv_pro:
                String url = "file:///android_asset/reg.html";
                Bundle bundle = new Bundle();
                bundle.putString("title", "用户协议");
                bundle.putString("url", url);
                ActivityUtil.next(getActivity(), WebViewActivity.class, bundle);
                break;
        }
    }

    /**
     * 密码登录
     *
     * @param phone
     * @param pwd
     */
    private void setLogin(final String phone, String pwd) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("username", phone);
        maps.put("password", pwd);
        maps.put("type", 1);
        HttpUtils.getInstance(getActivity()).postAsnyRequest(UrlUtils.MSFQ_URL_LOGIN, HttpUtils.getMaps(maps),
                new ResponseUtils() {
                    @Override
                    public void onSucceed(String code, JSONObject request) {
                        LogUtils.i("TAG:", "login=" + request);
                        data = (UserInfoBean) GsonUtils.getGsonData(UserInfoBean.class, request.toString());
                        if (data != null) {
                            JPushUtils.getInstance().setJPushAlias(phone);
                            SPUtils.putUserInfo(data);
                            getActivity().finish();
                        }
                    }

                    @Override
                    public void onFailure(String result) {
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 页面埋点
        StatService.onPageStart(getActivity(), "验证码登录");
    }

    @Override
    public void onPause() {
        super.onPause();
        // 页面埋点
        StatService.onPageEnd(getActivity(), "验证码登录");
    }


}
